<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>

        <template #right>
          <!-- $router.push(
            {
              path:'/import',
              query:{
                type:'user'
              }
            }
          ) -->
          <el-button
            type="warning"
            size="small"
            @click="$router.push('/import?type=user')"
          >excel导入</el-button>
          <el-button
            type="danger"
            size="small"
            @click="exportExcel"
          >excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showAddEmployeeDialog"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card v-loading="isLoading" style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column
            label="序号"
            type="index"
            width="100"
            :index="indexMethod"
          />
          <el-table-column label="头像" prop="staffPhoto">
            <template #default="{ row }">
              <img v-imgerror="errorImg" :src="row.staffPhoto||defaultImg" class="avatar_photo" @click="showImgDialogFn(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
            :formatter="getEmploymentText"
          >
            <!-- <template #default="{row}">
              {{ getEmploymentText(row.formOfEmployment) }}
            </template> -->
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template #default="{ row }">
              {{ row.timeOfEntry | getFormateTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{ row }">
              <!-- <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button> -->
              <!-- v-if="checkPermission('EMP_LOOK')" -->
              <el-button
                v-permission="'EMP_LOOK'"
                type="text"
                size="small"
                @click="$router.push(`/employees/detail/${row.id}`)"
              >查看
              </el-button>
              <!-- v-if="checkPermission('EMP_ROLE')" -->
              <el-button
                v-permission="'EMP_ROLE'"
                type="text"
                size="small"
                @click="showEditRoleDialogFn(row.id)"
              >角色</el-button>
              <!-- v-if="checkPermission('EMP_DELETE')" -->
              <el-button
                v-permission="'EMP_DELETE'"
                type="text"
                size="small"
                @click="delEmployee(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :current-page="page"
            :page-size="size"
            :total="total"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <addEmployee :show-dialog.sync="showDialog" />
      <el-dialog title="二维码图片" :visible="showCodeDialog" :close-on-click-modal="false" @close="hideCodeDialogFn">
        <el-row type="flex" justify="center">
          <canvas ref="myCanvas" />
        </el-row>
      </el-dialog>
      <assignRole :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
    </div>
  </div>
</template>

<script>
import { getUserListApi, delEmployeeApi } from '@/api/employees'
import enumObj from '@/constant/employees'
import addEmployee from './components/add-employee.vue'
import assignRole from './components/assign-role.vue'
import { getFormateTime } from '@/utils'
import errorImg from '@/assets/common/404.png'
import QrCode from 'qrcode'
import { mapGetters } from 'vuex'
// 导入
import hello from '@/mixins/hello'
import check from '@/mixins/check'
// import dayjs from 'dayjs'
export default {
  name: 'Employees',
  components: {
    addEmployee,
    assignRole
  },
  // filters: {
  //   getFormateTime(val) {
  //     return dayjs(val).format('YYYY-MM-DD')
  //   }
  // },
  // 混入
  mixins: [hello, check],
  data() {
    return {
      page: 1,
      size: 5,
      list: [],
      total: 0,
      isLoading: false,
      hireType: enumObj.hireType,
      showDialog: false,
      defaultImg: 'https://img2.baidu.com/it/u=2203692359,101708973&fm=253&fmt=auto&app=138&f=PNG?w=401&h=401',
      errorImg,
      showCodeDialog: false,
      showRoleDialog: false,
      userId: ''
    }
  },
  computed: {
    ...mapGetters(['roles'])
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      this.isLoading = true
      const {
        data: { rows, total }
      } = await getUserListApi(this.page, this.size)
      this.list = rows
      this.total = total
      this.isLoading = false
    },
    handleCurrentChange(val) {
      this.page = val
      this.getUserList()
    },
    indexMethod(index) {
      return (this.page - 1) * this.size + index + 1
    },
    // getEmploymentText(num) {
    //   return num === 1 ? '正式' : '非正式'
    // }
    getEmploymentText(row, column, cellValue, index) {
      const obj = this.hireType.find((item) => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    /**
     * 删除员工
     * @param {*} id
     */
    delEmployee(id) {
      this.$confirm('亲！您确定要删除吗？', '温馨提示')
        .then(async() => {
          await delEmployeeApi(id)
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.$message.success('恭喜！删除成功')
          this.getUserList()
        })
        .catch(() => {})
    },
    showAddEmployeeDialog() {
      this.showDialog = true
    },
    /**
     * 导出excel
     */
    async exportExcel() {
      // 先获取所有数据
      const {
        data: { rows }
      } = await getUserListApi(1, this.total)
      const headersArr = ['姓名', '手机号', '入职日期', '聘用形式', '转正日期', '工号', '部门']
      const multiHeader = [['姓名', '主要信息', '', '', '', '', '部门']]
      const merges = ['A1:A2', 'B1:F1', 'G1:G2']
      const headersRelations = {
        姓名: 'username',
        手机号: 'mobile',
        入职日期: 'timeOfEntry',
        聘用形式: 'formOfEmployment',
        转正日期: 'correctionTime',
        工号: 'workNumber',
        部门: 'departmentName'
      }
      const exportData = this.formateExportData(rows, headersArr, headersRelations)
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          // 表头
          header: headersArr,
          // 数据 二维数组
          data: exportData,
          // 文件名
          filename: '员工信息',
          autoWidth: true,
          // 文件后缀名
          bookType: 'xlsx',
          multiHeader,
          merges
        })
      })
    },
    /**
     * 处理数据中格式为二维数组
     * @param {*} rows  所有的数据
     * @param {*} headersArr 标头
     * @param {*} headersRelations 对应关系
     */
    formateExportData(rows, headersArr, headersRelations) {
      const result = []
      rows.forEach((item) => {
        const arr = []
        headersArr.forEach(key => {
          const englishKey = headersRelations[key]
          let value = item[englishKey]
          if (['timeOfEntry', 'correctionTime'].includes(englishKey)) {
            value = item[englishKey] ? getFormateTime(item[englishKey]) : '' // yyyy-mm-dd
          }
          if (englishKey === 'formOfEmployment') {
            const obj = this.hireType.find(enumObj => enumObj.id === item[englishKey])
            value = obj ? obj.value : '未知'
          }
          arr.push(value)
        })
        result.push(arr)
      })
      // console.log(result)
      return result
    },
    showImgDialogFn(url) {
      if (!url) {
        this.$message.error('当前用户没有头像')
        return
      }
      this.showCodeDialog = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.myCanvas, url)
      })
    },
    hideCodeDialogFn() {
      this.showCodeDialog = false
    },
    /**
     * 展示修改角色的弹框
     */
    showEditRoleDialogFn(id) {
      this.showRoleDialog = true
      this.userId = id
    }
  }
}
</script>

<style lang="scss" scoped>
  .avatar_photo{
    width:70px;
    height:70px;
    display: block;
    margin:0 auto;
    border-radius: 50%;
  }
</style>
